#! /usr/local/bin/perl

$font = shift(@ARGV); $block = substr($font,-1);
die "Missing font name" if ($font eq "");

@sizes = @ARGV;
@sizes = (1..7) if ($#sizes < 0);

$ENV{PATH} = "/u/slevy:$ENV{PATH}";

@mag = (0, 100, 120, 144, 173, 207, 248, 298);

print "Texing $font...\n";
system "amstex $font > /dev/null";
foreach $size (@sizes)
{
  print "\nMaking font $font-p$size...\n";
  $r = 1.2**($size-3);
  system "dvips -E -D$mag[$size] -o $font.ps $font";
  ($junk,$lx,$ly,$w,$h) = split(' ',`grep BoundingBox $font.ps`);
  system "echo quit | gs -sDEVICE=ppmraw -sOutputFile=tmp.ppm -dNOPAUSE -q " .
     "-r$mag[$size] -g".int(2*$r*$w+1.5)."x".int(2*$r*$h+1.5).
     " $font.ps > /dev/null";

  $gif = "$font-p$size.gif";
  system "convert -crop 0x0 tmp.ppm $gif";
  system "convert -crop +1+1 $gif $gif";
  system "convert -crop 0x0 $gif $gif";
  system "img2gif $gif $gif";
  unlink "tmp.ppm"; unlink "$font.ps";

  $line = `imginfo $gif | grep $font`;
  ($w,$h) = ($line =~ m/gif\s+(\d+)\s+(\d+)/);

  open(TEX,"$font.log"); @lines = <TEX>; close(TEX); chop(@lines);
  do {$line = shift(@lines)} while ($line ne "--start--");
  do {$line = pop(@lines)} while ($line ne "--end--");
  $height = pop(@lines); $height =~ s/pt//;
  $r = $h/$height; $H = int($r*12.0+.5);

  open(TAB,">$font-p$size.tab");
  print TAB "#Symbol Font -- block $block, size $size\n";
  print TAB "#",`date`;
  $n = ($block + 99)*10; $Y = 0;
  foreach $line (@lines)
  {
    $line =~ s/pt\s*/ /g;
    ($y,$h,$d,$w) = split(' ',$line);
    $y = int($r*($y-$h)+.5); $h = int($r*($h+$d)+.5);
    $d = int($r*$d+.5); $w = int($r*$w+.5);

    $y -= 1; $h += 3; $d += 2;  # in case there are roundoff errors,
				# get an extra pixel above and 2 below

    $y -= int($size/2); $h += int($size/2);
       # add space above for hats, arrows, etc.

    print TAB "$n=$H 0 ",$y-$Y," $h $w 0 $d\n";
    $n++; $Y += $H;
  }
  close(TAB);
}

unlink "$font.dvi";
unlink "$font.log";
